var base_url = getApp().globalData.base_url
var app = getApp()
Page({
    data: {
        oldNickname: '',
        newNickname: '',
    },

    onConfirm() {
        let userId = app.globalData.userId
        let nickname = this.data.newNickname
        // TODO 判断内容是字符串，并且长度小于10
        if (nickname.length > 10) {
            wx.showToast({
                title: '输入昵称过长',
                icon: 'false',
                duration: 1000//持续的时间
              })
            // TODO 提醒用户输入金额错误
            return
        }
        wx.request({
            url: base_url + '/users/profiles',
            method: 'PUT',
            header: {
                "content-type": 'application/x-www-form-urlencoded'
            },
            data: {
                userId: userId,
                nickname: nickname
            },
            success(res) {
                if (res.data.resultCode == 200) {
                    console.log(res.data.message)
                    app.globalData.nickname = nickname
                    wx.showToast({
                        title: '修改昵称成功',
                        icon: 'success',
                        duration: 1000//持续的时间
                      })
                    // TODO 弹出修改成功提示，跳转回主页面
                    wx.navigateBack({
                        delta: 0,
                    })
                } else {
                    wx.showToast({
                        title: '修改昵称失败',
                        icon: 'false',
                        duration: 1000//持续的时间
                      })
                }
            }
        })
    },

    onCurrentInput(e) {
        this.setData({
            newNickname: e.detail.value
        })
    },
    

    onLoad() {
        this.setData({
            oldNickname: app.globalData.nickname
        })
    }
});
